<template>
    <van-nav-bar title="我的订单" left-arrow @click-left="onBack" />

    <!-- 标签 -->
    <van-tabs v-model:active="state.active" @change="onTab">
        <van-tab v-for="item in state.tarList" :title="item.title" :name="item.name" :key="item.name">
        </van-tab>
    </van-tabs>

    <!-- 订单列表 -->
    <div class="list-box" v-if="state.OrderList.length > 0">
        <div class="list">
            <!-- 一个item代表一个订单 -->
            <div class="item" v-for="item in state.OrderList" :key="item.id">
                <div class="header">
                    <div class="left">订单号：<span>{{ item.code }}</span></div>
                    <div class="right">{{ item.status_text }}</div>
                </div>
                <div class="content">
                    <van-card v-for="son in item.OrderProduct" :key="son.id" :num="son.nums" :price="son.total" desc="描述信息"
                        :title="son.products.name" :thumb="son.products.avatars_cdn[0]">
                        <template #desc>
                            <span>商品单价：￥{{ son.price }}</span>
                        </template>
                    </van-card>
                </div>
                <div class="footer">
                    <div class="date">{{ item.createtime_text }}</div>
                    <div class="btn">
                        <van-button type="primary" size="mini" color="#7232dd"
                            :to="{ path: '/order/order/info', query: { orderid: item.id } }">订单详情</van-button>
                        <van-button type="primary" size="mini" v-if="item.status >= 1 && item.status <= 4">申请售后</van-button>
                        <van-button type="primary" size="mini" v-if="item.status == 0">确认支付</van-button>
                        <van-button type="primary" size="mini"
                            :to="{ path: '/order/order/logistics', query: { OrderNum: item.code } }"
                            v-if="(item.status >= 2 && item.status <= 4) || item.status < 0">查看物流</van-button>
                        <van-button type="primary" size="mini" v-if="item.status == 2"
                            @click="OrderConfirm(item.id)">确认收货</van-button>
                        <van-button type="primary" size="mini" v-if="item.status == 3"
                            @click="router.push({ path: '/order/order/rate', query: { orderid: item.id } })">立即评价</van-button>
                        <van-button type="danger" size="mini" v-if="item.status == 4" @click="delOrder(item.id)">删除订单</van-button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <van-empty description="暂无订单" v-else />
</template>
<script setup>
import { reactive, onMounted } from 'vue'
import { useUserStore } from '@/store/user.js'
import { storeToRefs } from 'pinia'
import apiList from '@/api'
import { useRouter } from 'vue-router'
import resJudge from '@/hooks/res.js'
import { showConfirmDialog } from 'vant'

const router = useRouter()
const userStore = useUserStore()
const { userInfo } = storeToRefs(userStore)

const state = reactive({
    active: '',
    tarList: [
        {
            title: '全部',
            name: ''
        },
        {
            title: '待支付',
            name: 0
        },
        {
            title: '待发货',
            name: 1
        },
        {
            title: '待收货',
            name: 2
        },
        {
            title: '待评价',
            name: 3
        },
        {
            title: '已评价',
            name: 4
        },
        {
            title: '退货中',
            name: -2
        },
        {
            title: '已退货',
            name: -1
        },
        {
            title: '退货被拒',
            name: -3
        },
    ],
    OrderList: [],
    LoginAuth: {}
})

onMounted(() => {
    getOrderData()
})

function onBack() {
    router.push('/business/base/index')
}
function onTab(value) {
    getOrderData()
}
async function getOrderData() {
    let data = {
        busid: userInfo.value.id
    }

    if (state.active !== '') {
        data.status = state.active
    }

    let result = await apiList.OrderIndex(data)

    if (result.code === 1) {
        state.OrderList = result.data
    }
}

//确认收获
function OrderConfirm(orderid) {
    showConfirmDialog({
        // title: '注销',
        message: '确定收货吗？',
    })
        .then(async () => {
            let res = await apiList.OrderConfirm({ orderid })
            resJudge(res)
            getOrderData()
        }).catch(()=>{

        })
}
//删除订单
function delOrder(orderid){
    showConfirmDialog({
        // title: '注销',
        message: '确定删除订单吗？',
    })
    .then(async () => {
        let res = await apiList.delOrder({ orderid,busid:userInfo.value.id })
        resJudge(res)
        getOrderData()
    }).catch(()=>{

    })
}
</script>

<style scoped>
.list-box {
    background: #ccc;
}

.list-box .header {
    display: flex;
    justify-content: space-between;
    /* padding: 0 10px; */
}

.list .item .left span {
    font-size: 14px;
    font-weight: bold;
}

.item {
    padding: 5px 5px 3px;
    background: #fff;
    margin-bottom: 5px;
}

.item .header .left {
    font-size: 13px;
}

.item .footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.item .footer .date {
    font-weight: bold;
}
</style>